﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using Sunxsh.Es.Common.Util;

namespace Sunxsh.Es.Web.Core.Filters;


/// <summary>
/// 全局异常过滤器，转换部分异常为前端可识别的信息
/// </summary>
class GlobalExceptionFilter : IExceptionFilter
{

    /// <inheritdoc />
    public void OnException(ExceptionContext context)
    {
        LoggerHelper.Error(context.Exception, context.Exception.Message);

        //var error = context.Exception;
        //if (error is AuthException authError)
        //{
        //    var result = new
        //    {
        //        State = authError.State.ToString(),
        //        ErrId = errId,
        //    };

        //    redirectError(435, result);
        //}
        //else if (error is OperationException opErr)
        //{
        //    var msg = error.Message;
        //    if (error.InnerException != null)
        //    {
        //        msg = error.InnerException.Message;
        //    }

        //    var result = new
        //    {
        //        Message = msg,
        //        ErrId = errId,
        //        opErr.NeedConfirm,
        //    };
        //    redirectError(436, result);
        //}

        var result = new 
        {
            StatusCode = 500,
            Errors = context.Exception.Message
        };
        context.Result = new JsonResult(result);
        context.HttpContext.Response.StatusCode = 500;
        context.ExceptionHandled = true;
    }
}